package deployr.cli;

import deployr.core.*;

public class CreateProjectResponder extends Responder {

	private final Workspace workspace;
	private final Projects projects;

	public CreateProjectResponder(Workspace workspace, Projects projects) {
		this.workspace = workspace;
		this.projects = projects;
	}

	@Override
	protected boolean shouldRespond(String method, String requestURI) {
		return "POST".equals(method) && requestURI.endsWith("/projects");
	}

	@Override
	protected Result respondTo(String method, String requestURI, Input input) {
		if (null != input.getParameters().get("appId")) {
			String appId = ((String[]) input.getParameters().get("appId"))[0];
			
			projects.createAndAddProject(new AppId(appId));
		} else {
			String groupId = ((String[]) input.getParameters().get("groupId"))[0];
			String artifactId = ((String[]) input.getParameters().get("artifactId"))[0];
			String version = ((String[]) input.getParameters().get("version"))[0];

			projects.createAndAddProject(groupId, artifactId, version);
		}
		return new Result(200);
	}

}
